<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/14
 * Time: 16:50
 */

namespace Admin\Controller;


class GoodsController extends AdminController
{
    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $where=array();
        $keyword=I('keyword');
        if($keyword){
            $where['keyword']=array('like','%'.$keyword.'%');
        }
        $group_id=M('auth_group_access')->where(array('uid'=>UID))->find();
        //审核权限
        if(UID!=1){
            if($group_id['group_id']==10){
                $member_info=M('member')->where(array('uid'=>$group_id['uid']))->find();
                $where['shop_id']=$member_info['shop_id'];
            }
        }
        $listsCount = M('goods')->where($where)->count();
        $Page       = new \Think\Page($listsCount,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show       = $Page->show();// 分页显示输出
        $lists = M('goods')->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($lists as $ke=>$ve){
            $goods_img_id=explode(',',$ve['goods_img']);
            $goods_img=[];
            foreach ($goods_img_id as $v){
                $img_info=M('picture')->where(array('id'=>$v))->find();
                $goods_img[]="http://".$_SERVER['SERVER_NAME'].$img_info['path'];
            }
            $lists[$ke]['goods_img']=$goods_img;
            $shop_info=M("shop")->where(array('id'=>$ve['shop_id']))->find();
            $lists[$ke]['shop_name']=$shop_info["shop_name"];
            $lists[$ke]['update_time']=date('Y-m-d H:i:s',$ve['update_time']);
        }
        $this->assign('group_id',$group_id['group_id']);
        $this->assign('list',$lists);
        $this->assign('_page',$show);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $dde = D('Goods')->create();
            $_POST["create_time"]=time();
            $_POST["update_time"]=time();
            if($dde){
                D('Goods')->add($_POST);
                $this->success('添加成功',U('Goods/index'));
            }else{
                $this->error(D('Goods')->getError());
            }
        }else{
            $uid=session('user_auth.uid');
            //审核权限
            if($uid!=1){
                $grout_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
                if($grout_id['group_id']==10){
                    $member_info=M('member')->where(array('uid'=>$grout_id['uid']))->find();
                    $shop_info=M("shop")->where(array('id'=>$member_info['shop_id']))->find();
                }
            }
            $this->assign('shop_id',$shop_info['id']);
            $this->assign('shop_name',$shop_info['shop_name']);
            $tree = $this->getTree(0,'id,title,sort,pid,level,status');
            $this->assign('tree', $tree);
            $this->display();
        }
    }

    public function edit(){
        if(IS_POST){
            $dde = D('Goods')->create();
            $_POST["create_time"]=time();
            $_POST["update_time"]=time();
            if($dde){
                D('Goods')->where(array("id"=>$_POST['id']))->save($_POST);
                $this->success('修改成功',U('Goods/index'));
            }else{
                $this->error(D('Goods')->getError());
            }
        }else{
            $uid=session('user_auth.uid');
            //审核权限
            if($uid!=1){
                $grout_id=M('auth_group_access')->where(array('uid'=>$uid))->find();
                if($grout_id['group_id']==10){
                    $member_info=M('member')->where(array('uid'=>$grout_id['uid']))->find();
                    $shop_info=M("shop")->where(array('id'=>$member_info['shop_id']))->find();
                }
            }
            $this->assign('shop_id',$shop_info['id']);
            $this->assign('shop_name',$shop_info['shop_name']);
            $id=I('id');
            $list=M('Goods')->where(array('id'=>$id))->find();
            if($list){
                $goods_img_id=explode(',',$list['goods_img']);
                $goods_img=[];
                foreach ($goods_img_id as $k=>$v){
                    $img_info=M('picture')->where(array('id'=>$v))->find();
                    $goods_img[$k]['url']="http://".$_SERVER['SERVER_NAME'].$img_info['path'];
                    $goods_img[$k]['id']=$v;
                }
                $list['goods_imgs']=$goods_img;
            }
            $tree = $this->getTree(0,'id,title,sort,pid,level,status');
            $this->assign('tree', $tree);
            $this->assign('list',$list);
            $this->display();
        }
    }

    /**
     * 显示分类树，仅支持内部调
     * @param  array $tree 分类树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function tree($tree = null){
        C('_SYS_GET_CATEGORY_TREE_');
        $this->assign('tree', $tree);
        $this->display('tree');
    }

    /**
     * 获取分类树，指定分类则返回指定分类极其子分类，不指定则返回所有分类树
     * @param  integer $id    分类ID
     * @param  boolean $field 查询字段
     * @return array          分类树
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function getTree($id = 0, $field = true){
        /* 获取当前分类信息 */
        if($id){
            $info = $this->info($id);
            $id   = $info['id'];
        }

        /* 获取所有分类 */
        $map  = array('status' => array('gt', 0));
        $list = M('goodsclassify')->where($map)->field($field)->order('sort asc')->select();
        $list = list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_', $root = $id);
        /* 获取返回数据 */
        if(isset($info)){ //指定分类则返回当前分类极其子分类
            $info['_'] = $list;
        } else { //否则返回所有分类
            $info = $list;
        }

        return $info;
    }
}